import axios from 'axios'
import {CONFIG} from '../config'

axios.defaults.timeout = 6000

axios.defaults.baseURL = CONFIG.API_BASE_URL

axios.interceptors.request.use(
  (config) => {
    config.headers = {
      'Content-Type': 'application/json;charset=UTF-8', // 传参方式json
    }
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)


axios.interceptors.response.use(
  (response) => {
    return response
  },
  (error) => {
    const { response } = error
    if (response) {
      
      _log(response.status) 
      return Promise.reject(response.data)
    } else {
      _log('网络连接异常,请稍后再试!')
    }
  }
)


export default function request(url = '', params = {}, type = 'POST') {
  
  return new Promise((resolve, reject) => {
    let promise
    if (type.toUpperCase() === 'GET') {
      promise = axios({
        url,
        params
      })
    } else if (type.toUpperCase() === 'POST') {
      promise = axios({
        method: 'POST',
        url,
        data: params
      })
    }
	promise.then((res) => {
        resolve(res)
      })
      .catch((err) => {
        reject(err)
      })
  })
}